/** 获取当前时间 */
function getFormatDate(format = 2) {
      var now = new Date();
      var year = now.getFullYear(); //得到年份
      var month = now.getMonth();//得到月份
      var date = now.getDate();//得到日期
      var day = now.getDay();//得到周几
      var hour = now.getHours();//得到小时
      var minu = now.getMinutes();//得到分钟
      var sec = now.getSeconds();//得到秒
      month = month + 1;
      if (month < 10) month = "0" + month;
      if (date < 10) date = "0" + date;
      if (hour < 10) hour = "0" + hour;
      if (minu < 10) minu = "0" + minu;
      if (sec < 10) sec = "0" + sec;
      var time = "";
      //精确到天
      if(format==1){
        time = year + "-" + month + "-" + date;
      }
      //精确到分
      else if(format==2){
        time = year + "-" + month + "-" + date+ " " + hour + ":" + minu + ":" + sec;
      }
      return time;
}

/** 获取随机形状 */
function  getRandomShape()
{
    var items = [
        'circle', // 圆形
        // 'triangle', // 三角形
        // 'square', // 正方形
    ];
    var randomItem = items[Math.floor(Math.random() * items.length)];

    return randomItem;

}

/** 获取随机颜色 */
function getRandomColor(){
    var color =  '#'+Math.floor(Math.random()*0xffffff).toString(16);
    return color;
}

/** 获取时间戳 */
function getTimestamp(offset){
    return Number(Date.parse(new Date())/1000) + Number(offset);
}

/** 获取随机头像 */

function getRandomAvatar(){
    var avatar = [
        '92fa9ced210eae37',
        'cbd80ff17a770868',
        'dd67956aee6dd5ac',
        '485227832e6bb8e2',
        '34aa28e93d51d1df',
        '40382a23e4d8dd0f',
        'd3296b4ddca6931d',
        '2ee13074cea00621',
        '0f1b1ca6b60b1480',
        '415323003b2f5239',
        '5e4e069984d0a788',
        'ee334f6c99cacab2',
        '7423cd03935721a4',
        '7e45f54643c85581',
        '9242562386f0bee4',
        'dd8bd8635576932f',
        'bbe6d3a8222733ca',
        'c454df24876c2716',
        'dc3b56c13028784c',
        'fb273a1a41836af5',
        'af9e0573b4f7e835',
        'ff7007a4948a591a',
        'd434562530a40abc',
        '8be185e216732c03',
        '2f39fdc6cecd2ef3',
        '99d7e823c7058651',
        'c717281b21a9a25b',
        '49b48b4f24a64304',
        '14063b0161f942a0',
        '6e9ee43a3b112171',
        '86131384bb69e6af',
        'bda6082501c2530a',
        '009808b4984814e1',
        '6819862a65cbfa96',
        '95cf179bac293049',
        'a99f32a4184f86a6',
        '37992d04a0d45ec6',
        '5a12efc93d836707',
        '94905ab000b44727',
        'a6afaff4c1e22804',
        'f837fd2bd592c234',
        'bb0f5d10e8eee889',
        '200fddfde92bd19c',
        '62cedc90d8191abe',
        'cc1cb03275462e9b',
        '0442be8d237e422b',
        '0af2a780bede82e1',
        '4deabd88832e7fb6',
        'a6bbefa952f48b2f',
        '4f9ed9560638ef03',
        '4d847eb1aab53b0c',
        '00739b20c776a0d4',
        'a0871c8520ca3595',
        'c6bfa3d064c2b310',
        'c49c913199e50710',
        '37ef82c8824eb399',
        '9467e5d151122398',
        '710893d7c36b383c',
        'a0531be7c1c2dd61',
        'f90499d25b9e122c',
        '2b91bceec1fe1716',
        '5615972887631743',
        'a4af6feae10ad545',
        'ecbc8959dea6385c',
        '96138bc59c192591',
        'dc67eb8fd0eb9cf3',
        'f0623001e6e998b1',
        'e58c04d3d67a6ea1',
        'f603a3240127248b',
        '3265b22297154391',
        'd33a6586b00674ae',
        'fff51cb1818125cb',
        'd0d684a743189fa0',
        '4b75b3102206d708',
        '14e0c7cccd3a026c',
        '026267f90f07d15e',
        'd5d5e190b5181ab7',
        'dd362cee134f0e1a',
        '9d0d088211ffb7ba',
        'e0247c4e82543713',
        'ac4c74d38a90b49d',
        'd07abbbc8c48a3d5',
        '29d70b8ed70e7655',
        '81ec65798e990120',
        '817a536db73da08a',
        '843914e839f3f561',
        '62a783de35b4c694',
        '8631af38d7a0d041',
        '0d1687c965b59e7d',
        'd6e8fe8c837c3221',
        'dcab4f4d6d3f6348',
        'ec3ae7a82471b99f',
        '11d3d53aa1b14914',
        'ae6cd8c4e8887252',
        'cea1e5154ef17ad0',
        '11aa167e0e432628',
        '01f8b88d3c1cfcd4',
        '8c8a7ba461c5530a',
        'c2e93e66db297244',
        'b8977d56b800d78a',
    ];

    var randomItem = avatar[Math.floor(Math.random() * avatar.length)];
    return 'avatar_'+randomItem;

}


/** 发送当前坐标信息 */
function sendPosition(){
    // 拖动时不发送坐标同步,防止诡异事件
    if(isDrag==true){
        return ;
    }
    console.log('发送坐标信息');
    var position = JSON.stringify({
        'user':clientId,
        'class':$('#myshape').attr('class'),
        'style':$('#myshape').attr('style'),
        'records':records
    });
    client.sendMessage(topic,position);
    records = [];
}


/** 移除过期用户信息 */
function removePosition()
{
    $('#public .shape').each(function(index,item){
        if(Number($(item).attr('data-deadline')) < Number(getTimestamp(0))){
           console.error(Number($(item).attr('data-deadline')));
           console.error(getTimestamp(0));
        $(item).remove();
        }
    })
}


/** 重置坐标信息 */
function resetPosition(){
    $('#myshape').css('top','');
    $('#myshape').css('left','');
    sendPosition()
    console.log('重置成功');
}


